define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pms/pmsreserveinfo/index' + location.search,
                    table: 'pms_reserve_info',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='room_type']", form).addClass("selectpage").data("source", "pms/pmsroomtype/selectpage");
                $("input[name='source']", form).addClass("selectpage").data("source", "pms/pmsconfig/selectpage").data("primaryKey", "id").data("field", "name").attr('data-params', '{"key":"source"}');
                $("input[name='room_info_id']", form).addClass("selectpage").data("source", "pms/pmsroominfo/selectpage").data("primaryKey", "id").data("field", "room_no");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'reserve_no', title: __('Reserve_no'), operate: 'LIKE'},
                        {field: 'source', title: __('Source'), operate: 'LIKE'},
                        {field: 'realname', title: __('Realname'), formatter: function(value, row, index){
                            var sex_name = '女士';
                            var sex_class = 'label-danger';
                            if (row['sex'] == 1)
                            {
                                sex_name = '先生';
                                sex_class = 'label-info';
                            }
                            return row['realname'] + ' <label class="label '+sex_class+'">'+sex_name+'</label>';
                        }, operate: 'LIKE'},
                        {field: 'tel', title: __('Tel'), operate: 'LIKE'},
                        {field: 'check_in_time', title: __('Check_in_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'check_out_time', title: __('Check_out_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'days', title: __('Days'), operate:'BETWEEN'},
                        {field: 'room_info_id', title: __('房间'), formatter:function (value, row, index){
                            return '<span class="label label-primary">'+row['room_type_name']+'</span><br />' + row['room_no'] + '房';
                        }},
                        {field: 'reserve_status', title: __('Reserve_status'), searchList: {"0":__('新预定'),"1":__('已转订单'),"-1":__('作废预定')}, formatter: Table.api.formatter.status},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'last_editor', title: __('Last_editor'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [{
                                name: '预定详情',
                                title: __('预定详情'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                text: '预定详情',
                                icon: 'fa fa-file-text-o',
                                extend: 'data-area=\'["80%","80%"]\'',
                                url: 'pms/pmsreserveinfo/detail'
                            }]}
                    ]
                ]
            });

            $(table).on("dblclick", "tr", function(e){
                e.preventDefault();
                $(this).find('.btn-dialog').click();
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            setTimeout(function(){
                $('.plupload-preview a.btn-trash').remove();
            }, 50);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});